<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;

class Index extends Controller
{
    /**
     * 后台登录页面
     * @return \think\response\View
     */
    public function index()
    {
        return view('admin@index/login');
    }

    /**
     * 后台登录处理
     * @return \think\response\View
     */
    public function logindo(Request $request)
    {

        // 接受数据
        $data = $request->post();
        $data['pwd'] = md5($data['pwd']);


        $res = Db::table('user')->field('id, nickname')->where('nickname', $data['name'])->where('pwd', $data['pwd'])->find();


        $id = $res['id'];
        if ($res > 0) {
            Session::set("admin.id", $id);
            Session::set("admin.name", $res['nickname']);
            // 查询当前账号的权限
            $list = Db::view('node','con_name,way')
                    ->view('grno','node_id','node.id=grno.node_id')
                    ->view('usgr','role_id','usgr.role_id=grno.role_id')
                    ->view('user','id','user.id=usgr.user_id')
                    ->where('user.id','=',$id)
                    ->select();


            foreach ($list as $key => $value) {
                $list[$key]['con_name'] = ucfirst($value['con_name']);
            }


            $nodelist = array();
            $nodelist['Main'] = array('index'); 
            foreach ($list as $v) {
                $nodelist[$v['con_name']][] = $v['way'];
                if ($v['way'] == "edit"){
                    $nodelist[$v['con_name']][] = "update";
                }
                if ($v['way'] == "add"){
                    $nodelist[$v['con_name']][] = "doadd";
                }
            }

            Session::set('admin.node', $nodelist);

            $this->success('登录成功', url("admin/Main/index"));
        } else {
            $this->error('信息有误,请重试');
        }
    }

    /**
     * 后台登出处理
     * @return \think\response\View
     */
    public function logout()
    {
        Session::delete('admin');

        $this->redirect('admin/Index/index');
    }

}
